﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using Microsoft.Windows.Controls;

using InfFront.InfoViews.Base;

namespace InfFront.InfoViews.WPF
{
    /// <summary>
    /// Interaction logic for SingleEditWithToolbarFrame.xaml
    /// </summary>
    public partial class SingleEditWithToolbarFrame : BaseSourceContentFrame
    {
        #region Private Variables
        #endregion

        #region Construction/Initialization
        public SingleEditWithToolbarFrame(InfoViewSource source)
        {
            InitializeComponent();
            Source = source;
        }
        #endregion

        public override void Rebuild()
        {
            commandsControlList.ItemsSource = null;
            currentEntryFrame.DataContext = null;

            if (Source == null || Source.IsDummy)
            {
                return;
            }

            var firstEntry = Source.GetFirstEntry();
            currentEntryFrame.DataContext = firstEntry;
            commandsControlList.ItemsSource = Source.Commands;

            int i = 0;
            foreach (bool cmdEnabled in Source.CheckCommandsEnabled(firstEntry))
            {
                var cmd = (InfoViewCommand)commandsControlList.Items[i++];
                cmd.IsEnabled = cmdEnabled;
            }
        }

        private void CommandButton_Click(object sender, RoutedEventArgs e)
        {
            var cmd = ((FrameworkElement)e.Source).DataContext as InfoViewCommand;
            if (cmd == null)
            {
                Debug.Assert(false, "No InfoViewCommand found");
                return;
            }
            var args = new InfoViewCommand.ExecutionArgs();
            args.CurrentEntry = currentEntryFrame.DataContext;
            cmd.Execute(args);
        }

    }
}
